package com.sevici;
import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sevici.model.Distrito;
import com.sevici.model.Estacion;

public class XMLHandlerLocaBici extends DefaultHandler{

	// ===========================================================
	// Campos
	// ===========================================================

	
	//Etiquetas
	@SuppressWarnings("unused")
	private boolean in_carto = false;
	@SuppressWarnings("unused")
	private boolean in_markers = false;
	@SuppressWarnings("unused")
	private boolean in_marker = false;
	@SuppressWarnings("unused")
	private boolean in_arrondissements = false;
	@SuppressWarnings("unused")
	private boolean in_arrondissement = false;
	
	
	@SuppressWarnings("unused")
	private boolean in_station = false;
	@SuppressWarnings("unused")
	private boolean in_available = false;
	
	@SuppressWarnings("unused")
	private boolean in_free = false;
	
	@SuppressWarnings("unused")
	private boolean in_total = false;
	
	@SuppressWarnings("unused")
	private boolean in_ticket = false;

	private ParsedXMLSevici myParsedXMLSevici = null;

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public ParsedXMLSevici getParsedData() {
		return this.myParsedXMLSevici;
	}

	// ===========================================================
	// Metodos
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedXMLSevici = new ParsedXMLSevici();
	}

	@Override
	public void endDocument() throws SAXException {
		// No hacemos nada
	}

	/** Se ejecuta cuando encuentra tags como:
	 * <tag>
	 * Puede leer atributos cuando se encuentra con:
	 * </tag><tag attribute="attributeValue">*/
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("carto")) {
			this.in_carto = true;
		}else if (localName.equals("markers")) {
			this.in_markers = true;
			this.myParsedXMLSevici.setEstaciones(new ArrayList<Estacion>());			
		}else if (localName.equals("marker")) {
			this.in_marker = true;
			this.myParsedXMLSevici.addEstacion(atts);			
		}else if (localName.equals("arrondissements")) {
			this.in_arrondissements = true;
			this.myParsedXMLSevici.setDistritos(new ArrayList<Distrito>());
		}else if (localName.equals("arrondissement")) {
			this.in_arrondissement = true;
			this.myParsedXMLSevici.addDistrito(atts);
		}else if (localName.equals("station")) {
			this.in_station = true;
		}else if (localName.equals("available")) {
			this.in_available = true;			
		}else if (localName.equals("ticket")) {
			this.in_ticket = true;			
		}else if (localName.equals("total")) {
			this.in_total = true;			
		}else if (localName.equals("free")) {
			this.in_free = true;			
		}
	}
	/** Se ejecuta en tags de cierre:
	 * </tag> */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
	throws SAXException {
		if (localName.equals("carto")) {
			this.in_carto = false;
		}else if (localName.equals("arrondissements")) {
			this.in_arrondissements = false;
		}else if (localName.equals("arrondissement")) {
			this.in_arrondissement = false;
		}else if (localName.equals("markes")) {
			this.in_markers = false;
		}else if (localName.equals("marke")) {
			this.in_marker = false;
		}else if (localName.equals("station")) {
			this.in_station = false;
		}else if (localName.equals("available")) {
			this.in_available = false;			
		}else if (localName.equals("ticket")) {
			this.in_ticket = false;			
		}else if (localName.equals("total")) {
			this.in_total = false;			
		}else if (localName.equals("free")) {
			this.in_free = false;			
		}
	}
	
	/** Se ejecuta cuando se encuentra con la siguiente estructura:
	 * <tag>characters</tag> */
	@Override
	public void characters(char ch[], int start, int length) {
		if(this.in_carto){
			//myParsedXMLSevici = new ParsedXMLSevici();
		}
		if(this.in_markers){
			//TODO			
		}
		if(this.in_arrondissements){
			//TODO
		}
		if(this.in_marker){
			//TODO
		}
		if(this.in_arrondissement){
			//TODO
		}
	}
}
